﻿using Ldt.DataEngine.RuleAssemble;
using LdtJudge.DataEngine.Common.CommHelp;
using LdtJudge.DataEngine.Model.DataModel.SysCommon;
using System;
using System.Collections.Generic;
using System.Linq;

namespace LdtDataEngin.ModuleDetail.AnalysisModule.CaseCauseBaseInfoExtr
{
    public class BCaseCauseExt
    {
        /// <summary>
        /// 民事案由提取
        /// </summary>
        /// <param name="caseCause">案由名称或者案由ID</param>
        /// <param name="OriginalText">原本内容</param>
        /// <param name="caseType">案件类型</param>
        /// <returns></returns>
        public IList<CaseCauseModel> CaseCauseExtract(string caseCause, string OriginalText, string caseType)
        {
            IList<Tuple<string, string>> crimeNameList = new List<Tuple<string, string>>();
            IList<CaseCauseModel> reList = new List<CaseCauseModel>();
            GetSysCaseCause dalCause = new GetSysCaseCause();

            string MacthKeyWord = string.Empty;
            if (string.IsNullOrEmpty(caseCause))
            {//如果案由为空 

                var CaseCauseOriginal = " CS_CaseCauseRelation$OriginalText$MSCourtRecord";
                List<string> listOriginalText = new List<string>();
                listOriginalText.Add(OriginalText);
                var OriginalrowList = RuleParseClass.GetParseRegCodeList(CaseCauseOriginal, listOriginalText);
                //提取到多个案由
                var CauseNameCode = " CS_CaseCauseRelation$CH_CaseCauseName$MSCourtRecord";

                string originalText = OriginalrowList.Count > 0 ? OriginalrowList[0] : OriginalText;
                var rowList = RuleParseClass.GetParseRegCodeList(CauseNameCode, new List<string> { originalText });

                foreach (string cuseName in rowList)
                {
                    if (!crimeNameList.Any(tup => tup.Item1 == cuseName))
                    {
                        crimeNameList.Add(new Tuple<string, string>(cuseName, originalText));
                    }
                }
            }
            else
            {
                crimeNameList.Add(new Tuple<string, string>(caseCause, "平台传入的案由，不进行案由提取！Common"));
            }

            foreach (Tuple<string, string> causeName in crimeNameList)
            {
                //0001.根据案由集合，进行根据案由名称匹配
                CaseCauseModel MCause = dalCause.GetModelByCauseName(causeName.Item1, caseType);
                if (MCause == null)
                {
                    /// 0002.根据案由集合，进行根据案由的ID匹配
                    MCause = dalCause.GetModel(causeName.Item1);
                }
                if (MCause == null)
                {
                    /// 0003.根据案由集合，进行根据案由名称模糊匹配
                    string causeID = dalCause.GetCauseID(causeName.Item1, "b");
                    MCause = dalCause.GetModel(causeID);
                }
                if (MCause != null)
                {
                    MCause.MatchOrginalText = causeName.Item2;
                    reList.Add(MCause);
                }
            }
            return reList;
        }
    }
}
